﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using TIMETABLEASY.iCalManager;

using TIMETABLEASY.EasyCalendar.Model;
using TIMETABLEASY.EasyCalendar.Controler;

using TIMETABLEASY.TokenAuthentification;

namespace TIMETABLEASY
{
    public partial class Window1
    {
        #region fonctions teachers
        ////////////////////////FONCTIONS TEACHERS////////////////////////////////


        //Listing des profs enregistrés
        private void list_teachers_offline(ListBox maListe)
        {
            maListe.Items.Clear();

            foreach (Teacher s in (from e in db_local.Teacher select e))
            {
                maListe.Items.Add(s.Login);

            }
        }


        //Affichage de la liste des profs
        private void display_teachers_offline(String teachers)
        {
            foreach (Teacher s in (from e in db_local.Teacher select e))
            {
                if (s.Login == teachers)
                {
                    teacher_login_textbox.Text = s.Login;
                    teacher_password_textbox.Password = s.Password;
                    teacher_firstname_textbox.Text = s.Firstname;
                    teacher_surname_textbox.Text = s.Surname;

                    if (s.IsStudent == 1)
                    {
                        teacher_status_checkbox.IsChecked = true;

                        teacher_campus_combobox.Items.Clear();
                        chargement_liste_campus_teacher_offline();
                        teacher_campus_combobox.SelectedValue = s.Campus;

                        teacher_class_combobox.Items.Clear();
                        teacher_class_combobox.Items.Add(s.Classe);
                        teacher_class_combobox.SelectedValue = s.Classe;
                    }
                    else
                    {
                        teacher_status_checkbox.IsChecked = false;
                    }

                }

            }
        }


        //Rafraichissement des éléments
        private void refresh_teachers_offline()
        {
            teacher_login_textbox.Text = "";
            teacher_password_textbox.Password = "";
            teacher_firstname_textbox.Text = "";
            teacher_surname_textbox.Text = "";
            list_teachers(teacher_listbox);
            chargement_liste_campus_teacher_offline();
        }


        //Ajout d'un prof
        private void add_teachers_offline()
        {
            Teacher teacher1 = new Teacher();

            teacher1.Login = teacher_login_textbox.Text.ToLower();
            teacher1.Password = teacher_password_textbox.Password;
            teacher1.Firstname = teacher_firstname_textbox.Text.ToLower();
            teacher1.Surname = teacher_surname_textbox.Text.ToLower();

            teacher1.Notepad_enabled = 1;
            teacher1.Notepad_text = "Write some notes here";

            if (teacher_status_checkbox.IsChecked.Value)
            {
                teacher1.IsStudent = 1;
                teacher1.Campus = teacher_campus_combobox.SelectedItem.ToString().ToLower();
                teacher1.Classe = teacher_class_combobox.SelectedItem.ToString().ToLower();
            }
            else
            {
                teacher1.IsStudent = 0;
            }


            db_local.Teacher.InsertOnSubmit(teacher1);
            db_local.SubmitChanges();

            //Rafraichissement des éléments
            refresh_teachers_offline();


        }


        //Mise à jour d'un prof
        private void update_teachers_offline()
        {
            if (teacher_login_textbox.Text != "" && teacher_password_textbox.Password != "" && teacher_firstname_textbox.Text != "" && teacher_surname_textbox.Text != "")
            {
                Teacher teacher2 = (from e in db_local.Teacher
                                    where e.Login == (string)teacher_listbox.SelectedValue
                                    select e).FirstOrDefault();

                teacher2.Login = teacher_login_textbox.Text.ToLower();
                teacher2.Password = teacher_password_textbox.Password;
                teacher2.Firstname = teacher_firstname_textbox.Text.ToLower();
                teacher2.Surname = teacher_surname_textbox.Text.ToLower();

                if (teacher_status_checkbox.IsChecked.Value)
                {
                    teacher2.IsStudent = 1;
                    teacher2.Campus = teacher_campus_combobox.SelectedItem.ToString().ToLower();
                    teacher2.Classe = teacher_class_combobox.SelectedItem.ToString().ToLower();
                }
                else
                {
                    teacher2.IsStudent = 0;
                }

                db_local.SubmitChanges();

                //Rafraichissement des éléments
                refresh_teachers_offline();
            }

        }


        //Suppression d'un prof
        private void delete_teachers_offline()
        {
            if (teacher_login_textbox.Text != "")
            {
                Teacher teacher3 = (from e in db_local.Teacher
                                    where e.Login == (string)teacher_listbox.SelectedValue
                                    select e).FirstOrDefault();

                db_local.Teacher.DeleteOnSubmit(teacher3);
                db_local.SubmitChanges();

                //Rafraichissement des éléments
                refresh_teachers_offline();
            }

        }


        private void sort_teachers_by_campus_offline()
        {

            string current_selected_campus;

            //On affiche les bons événéments selon le tri effectué
            if (teacher_sort_combobox.Text != "")
            {
                current_selected_campus = teacher_sort_combobox.SelectedValue.ToString();

                teacher_listbox.Items.Clear();

                if (current_selected_campus == "All")
                {
                    foreach (Teacher teacher1 in (from e in db_local.Teacher
                                               select e))
                    {
                        teacher_listbox.Items.Add(teacher1.Login);
                    }

                }

                else
                {
                    foreach (Teacher teacher1 in (from e in db_local.Teacher
                                               where e.Campus == current_selected_campus
                                               select e))
                    {
                        teacher_listbox.Items.Add(teacher1.Login);
                    }
                }

            }

        }


        private void chargement_liste_campus_teacher_offline()
        {
            teacher_campus_combobox.Items.Clear();

            foreach (Campus s in (from e in db_local.Campus select e))
            {
                teacher_campus_combobox.Items.Add(s.Name);
            }
        }

        private void chargement_liste_classe_teacher_offline()
        {
            int id_class_temp = (from i in db_local.Campus
                                 where i.Name.Equals(teacher_campus_combobox.SelectedValue)
                                 select i.Id).FirstOrDefault();

            teacher_class_combobox.Items.Clear();

            foreach (Classes s in (from e in db_local.Classes where e.Campus_id == id_class_temp select e))
            {
                teacher_class_combobox.Items.Add(s.Name);
            }

        }


        private void chargement_campus_autorises_teacher_offline()
        {

            switch (usertype)
            {
                case "administrators":

                    teacher_sort_combobox.Items.Clear();
                    teacher_sort_combobox.Items.Add("All");

                    foreach (Campus campus in (from e in db_local.Campus select e))
                    {
                        teacher_sort_combobox.Items.Add(campus.Name);
                        teacher_sort_combobox.SelectedItem = teacher_sort_combobox.Items[0];
                    }

                    break;

                case "managers":

                    teacher_sort_combobox.Items.Clear();

                    //Récupération, découpage et affichage des campus enregistrés pour un manager

                    string[] table_campus;
                    string registred_campus = usercampus;
                    table_campus = registred_campus.Split('|');

                    for (int i = 0; i < table_campus.Length; i++)
                    {
                        teacher_sort_combobox.Items.Add(table_campus[i]);
                    }

                    teacher_sort_combobox.SelectedItem = teacher_sort_combobox.Items[0];

                    break;

                case "students":

                    teacher_sort_combobox.Items.Clear();

                    teacher_sort_combobox.Items.Add(usercampus);
                    teacher_sort_combobox.SelectedItem = teacher_sort_combobox.Items[0];

                    break;

                case "teacher":

                    teacher_sort_combobox.Items.Clear();

                    teacher_sort_combobox.Items.Add(usercampus);
                    teacher_sort_combobox.SelectedItem = teacher_sort_combobox.Items[0];

                    break;

            }
        }


        #endregion
    }
}
